package com.kq.servlet.kqctrl;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.PreloadListener;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

@SuppressWarnings("serial")
public class dDiary extends HttpServlet
{
  public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
  {
    response.setCharacterEncoding("UTF-8");
    HtmlPage rootPage = PreloadListener.rootPage;

    String id = request.getParameter("id");
    String html = "false";
    String name = PreloadListener.loginInfo.get("name").toString();
    String pswd = PreloadListener.loginInfo.get("pswd").toString();
    String ajaxContent = "$.ajax({url:'http://erp.kanq.com.cn:806/pages/user_login.action',type:'get',dataType:'json',";
    ajaxContent = ajaxContent + "data:{'name':'" + name + "','pswd':'" + pswd + "'},async:false,cache:false,";
    ajaxContent = ajaxContent + "error: function(xhr){;";
    ajaxContent = ajaxContent + "$.ajax({url:'http://erp.kanq.com.cn:806/pages/ajaxrequest.jsp',type:'post',dataType:'json',";
    ajaxContent = ajaxContent + "data:{'data':\"SqlExecute(DELETE FROM kanq_diary where id = '" + id + "')\"},";
    ajaxContent = ajaxContent + "error: function(xhr,status,data){$('body').html(JSON.stringify(xhr.responseText));}})}});";
    rootPage.executeJavaScript(ajaxContent);
    try {
      Thread.sleep(100L);
    }
    catch (InterruptedException e) {
      e.printStackTrace();
    }
    html = rootPage.getBody().getTextContent();
    int i = 0;
    while (html.indexOf("SQL语句执行成功") == -1) {
      try {
        Thread.sleep(100L);
      }
      catch (InterruptedException e) {
        e.printStackTrace();
      }
      i++;
      if (i > 1000) {
        break;
      }
      System.out.println("执行了" + i + "次删除重试！");
      html = rootPage.getBody().getTextContent();
    }

    System.out.println("html=" + html);
    PrintWriter out = response.getWriter();
    out.write(html);
    out.flush();
  }
}